#!/usr/bin/env python
# -*- coding: utf-8 -*-

from lib.format_time import *
from lib.HandleMysql import *
from lib.handle_mongo import *
from lib.switch_code import *
from zhineng_conn import *
from config_tools import *
from imc_conn import *
from lib.load_data import *
import time
import math
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')
    
def format_timestamp(ts):
    ts = ts/1000
    return time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime(ts))
    
def get_views_from_mongo(date,s58):
    
    mongo_conn = set_visit_conn()
    print "mongodb connected"
    stat_conn = set_stats_conn()
    dict_conn = set_dict_conn()
    line_list = []
    stat_table='ad_views_stat'
    log_table = 'views_1'
    
    #sep='-'
    #fmt=sep.join(['%Y','%m','%d'])
    #today_ts = time.mktime(time.strptime(date, fmt ))
    #today_ts = today_ts*1000
    query = "select ifnull(max(view_time_id),0) from ad_views_stat;"
    items = stat_conn.get_results(query)
    min_id = items[0][0]
    print min_id
    max_id = min_id + 86400000
    if min_id == 0 :
        max_id = 1357826400000 + 86400000
        min_id = 1357826400000 - 1
    else:
        min_id = min_id-1
    
    query = {'viewTime':{'$gte':min_id,'$lt':max_id}}
    #print query
    items = find_items(mongo_conn,log_table,query)
    for item in items:
        log_id = item['_id']
        info_id = item['infoID']
        info_type = item['infoType']
        cate_id = item['lcate']
        city = item['lcity']
        user_id = item['pUID']
        plan_id = item['planID']
        unit_id = item['unitID']
        views = item['view']
        view_time_id = item['viewTime']
        
        view_time = format_timestamp(view_time_id)
        
        params = ''
        info = s58.get_info(info_id)
        if info != None:
            params = info.getParams()
        
        dc = get_thirth_cat(cate_id,params,dict_conn)    
        cat1 = 0
        cat1name = ''
        cat2 = 0
        cat2name=''
        cat3 = 0
        cat3name=''
        if dc != None:
            if len(dc) > 0:
                cat1 = dc[0][0]
                cat1name = dc[0][1]
            if len(dc) > 1:
                cat2 = dc[1][0]
                cat2name=dc[1][1]
            if len(dc) > 2:
                cat3 = dc[2][0]
                cat3name=dc[2][1]
                
        city_id = 0
        city_name = ''
        area_id = 0
        area_name = ''
        local_id= 0
        local_name = ''
        
        dc = get_citys(city,dict_conn)
        if dc != None:
            if len(dc) >0:
                city_id = dc[0][0]
                city_name = dc[0][1]
            if len(dc)>1:
                area_id = dc[1][0]
                area_name = dc[1][1]
            if len(dc)>2:
                local_id= dc[2][0]
                local_name = dc[2][1]
        param = [log_id,info_id,info_type,cate_id,city_id,city_name,area_id,area_name,local_id,local_name,cat1,cat1name,cat2,cat2name,cat3,cat3name,user_id,plan_id,unit_id,views,view_time,view_time_id]
        line_list.append('\t'.join(map(lambda i : str(i), param)))
        
    cont = '\n'.join(line_list)
    path = "/work/opt/analytic/lh_tmp_data/zhineng/daily/"
    file_name = "ad_views_stat%s.txt"%date.replace('-',"")
    writeTextToFile(path,file_name,"w",cont)    
    load_data(stat_conn,path+file_name,stat_table,'\t')
    
    dict_conn.close()
    stat_conn.close()

def get_clicks_from_mongo(date,s58):

    mongo_conn = set_visit_conn()
    print "mongodb connected"
    stat_conn = set_stats_conn()
    dict_conn = set_dict_conn()
    line_list = []
    stat_table='ad_clicks_stat'
    log_table = 'clicks_1'
    
    query = "select ifnull(max(click_time_id),0) from ad_clicks_stat;"
    items = stat_conn.get_results(query)
    min_id = items[0][0]
    max_id = min_id + 86400000
    if min_id == 0 :
        max_id = 1357826400000 + 86400000
        min_id = 1357826400000 - 1
    else:
        min_id = min_id-1
        
    #print min_id
    query = {'clickTime':{'$gte':min_id,'$lt':max_id}}
    
    #print query
    
    items = find_items(mongo_conn,log_table,query)
    for item in items:
        log_id = item['_id']
        bid_str = item['bid']
        bid = 0
        if bid_str != None and bid_str!='': 
            bid = float(bid_str)/100;
            
        click = item['click']
        click_time_id = item['clickTime']
        info_id = item['infoID']
        info_type = item['infoType']
        cate_id = item['lcate']
        city = item['lcity']
        user_id = item['pUID']
        plan_id = item['planID']
        unit_id = item['unitID']
        view_time_id = item['viewTime']
        
        #print click_time_id
        click_time = format_timestamp(click_time_id)
        view_time = format_timestamp(long(view_time_id))
        
        params = ''
        info = s58.get_info(info_id)
        if info != None:
            params = info.getParams()
        
        dc = get_thirth_cat(cate_id,params,dict_conn)    
        cat1 = 0
        cat1name = ''
        cat2 = 0
        cat2name=''
        cat3 = 0
        cat3name=''
        if dc != None:
            if len(dc) > 0:
                cat1 = dc[0][0]
                cat1name = dc[0][1]
            if len(dc) > 1:
                cat2 = dc[1][0]
                cat2name=dc[1][1]
            if len(dc) > 2:
                cat3 = dc[2][0]
                cat3name=dc[2][1]
                
        city_id = 0
        city_name = ''
        area_id = 0
        area_name = ''
        local_id= 0
        local_name = ''
        
        dc = get_citys(city,dict_conn)
        if dc != None:
            if len(dc) >0:
                city_id = dc[0][0]
                city_name = dc[0][1]
            if len(dc)>1:
                area_id = dc[1][0]
                area_name = dc[1][1]
            if len(dc)>2:
                local_id= dc[2][0]
                local_name = dc[2][1]
        param = [log_id,bid,click,click_time,info_id,info_type,cate_id,city_id,city_name,area_id,area_name,local_id,local_name,cat1,cat1name,cat2,cat2name,cat3,cat3name,user_id,plan_id,unit_id,view_time,click_time_id,view_time_id]
        line_list.append('\t'.join(map(lambda i : str(i), param)))
        
    cont = '\n'.join(line_list)
    #print cont
    path = "/work/opt/analytic/lh_tmp_data/zhineng/daily/"
    file_name = "ad_clicks_stat%s.txt"%date.replace('-',"")
    writeTextToFile(path,file_name,"w",cont)    
    load_data(stat_conn,path+file_name,stat_table,'\t')
    
    dict_conn.close()
    stat_conn.close()

def execute(date,s58):
    #get_views_from_mongo(date)
    try:
        pass
        get_views_from_mongo(date,s58)
    except Exception,e:
        print 'get_views_from_mongo:',e
    try:
        pass
        get_clicks_from_mongo(date,s58)
    except Exception,e:
        print 'get_clicks_from_mongo:',e

    
def main():

   #date = '2012-05-28'
   #execute(date)   
   if len(sys.argv)==1:
        start_date=get_yesterday()
        end_date=start_date
   elif len(sys.argv)==2:
        start_date=format_date(sys.argv[1])
        end_date=start_date
   elif len(sys.argv)==3:
        start_date=format_date(sys.argv[1])
        end_date=format_date(sys.argv[2])
   stat_date=start_date
   s58=Service58()
   while stat_date>=start_date and stat_date<=end_date:
        print "The program of the date of %s is beginning at %s " %(stat_date,get_now())
        #
        execute( stat_date,s58) 
        print "The program of the date of %s is end at %s " %(stat_date,get_now())
        stat_date=var_date(stat_date)


if __name__=='__main__':
    main()
    #print format_timestamp(1357826400000)
    
    #json.loads(local)[0]['L'] 
    
